package com.moon.task;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author thy
 * @description：todo
 * @date 2024/7/19 15:32
 */
@Slf4j
public class DataDealTask implements Callable<List<Integer>> {
    private List<Integer> data;
    private CountDownLatch latch;

    public DataDealTask(List<Integer> data, CountDownLatch latch) {
        this.data = data;
        this.latch = latch;
    }

    @Override
    public List<Integer> call() throws Exception {
        List<Integer> result = new ArrayList<>();
        try {
            // 模拟处理数据并返回结果
            for (Integer num : data) {
                Thread.sleep(ThreadLocalRandom.current().nextInt(1, 11)* 1000L);
                int processed = num * 2 + 100; // 例如：将数据乘以2作为处理结果
                result.add(processed);
                log.info("处理数据: " + num + " -> " + processed);
            }
        } finally {
            // 任务完成后调用 countDown
            latch.countDown();
        }
        return result;
    }

}
